<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use danielme85\Geoip2\Facade\Reader;

class GetShippingAddressCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'getshippingadderss';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '采集收货地址';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $url = "https://www.meiguodizhi.com/api/v1/dz";
        $c = ['en','ca','de','au','ru'];

        for ($i = 1;$i<5000;$i++){

            $str = $c[rand(0,4)];
            //$param = '{"city":"","path":"/kr-address","method":"refresh"}';
            $param = [
                "city" => "",
                "path" => "/{$str}-address",
                "method" => "refresh"
            ];

            $response = Http::asJson()->post($url, $param);
            $result = json_decode($response->body(), true);
            $address = $result['address']['Address'] .' '. $result['address']['City'] .' '.$result['address']['State_Full'];
            if(!empty($address)){
                $insert_data['country'] = $str;
                $insert_data['address'] = trim($address);
                DB::table('shipping_address')->insertOrIgnore($insert_data);
                //sleep(1);
            }
        }

    }









}
